package com.wqz.servlet;

import com.alibaba.fastjson.JSONObject;
import com.wqz.pojo.User;
import com.wqz.service.UserServiceDao;
import com.wqz.service.impl.UserServiceDaoImpl;

import javax.jws.WebService;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request, response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String userName = request.getParameter("userName");
        int gender = Integer.parseInt(request.getParameter("gender"));

        //System.out.println(userName + "-------" + gender);
        UserServiceDao userServiceDao = new UserServiceDaoImpl();
        List<User> userList = null;
        try {
            userList = userServiceDao.findUser(userName,gender);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //System.out.println(userList);
        //System.out.println("------------------------------");
        JSONObject jsonObject = new JSONObject();
        //System.out.println(jsonObject.toJSONString(userList));
        response.setCharacterEncoding("gbk");
        response.getWriter().print(jsonObject.toJSONString(userList));
    }
}
